package cn.tedu.controller;

import cn.tedu.cache.CacheConstants;
import cn.tedu.entity.Category;
import cn.tedu.mapper.CategoryMapper;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@Api(tags = "2.商品分类处理类")
@RestController
@RequestMapping("category")
public class CategoryController {
    @Autowired
    private CategoryMapper mapper;

    @CacheEvict(value = CacheConstants.CACHE2,allEntries = true)
    @PostMapping("insert")
    public void insert(@RequestBody Category category) {
        mapper.insert(category);
    }

    //添加缓存,value属性为添加到的缓存名字,sync表示异步
    @Cacheable(value = CacheConstants.CACHE2,sync = true)
    @GetMapping("list")
    public List<Category> list() {
        System.out.println("===获取数据===");
        return mapper.list();
    }

    //清空缓存,value属性为添加到的缓存名字,allEntries = true表示清空所有
    @CacheEvict(value = CacheConstants.CACHE2,allEntries = true)
    @DeleteMapping("delete/{id}")
    public void delete(@PathVariable int id) {
        mapper.deleteById(id);
    }
}
